﻿#include <QFile>
#include "Public.h"
#include "CGlobal.h"
#include "CSensorSeriesMain.h"
#include "CSensorSeriesList.h"
#include "CSensorSeriesAlarmQuery.h"
#include "CGlobal.h"
#include "CSensorSeriesHistory.h"
#include "CSensorSericesCfgOper.h"

CSensorSeriesMain::CSensorSeriesMain(CDevSensorSeries* pDev, QWidget *parent)
	:m_pDev(pDev),QWidget(parent)
{
	ui.setupUi(this);
	this->setWindowState(Qt::WindowMaximized);
	m_sShowGuidNow = pDev->m_pDevData->m_sDevGuid + "-" + QString::number(1);
	m_bIsRun = false;
	//setStyleSheetFile();
	initConnect();
	initUi();
}

CSensorSeriesMain::~CSensorSeriesMain()
{
}

void CSensorSeriesMain::initUi()
{
	QString sName = QString::fromLocal8Bit("声振温检测");
	setWindowTitle(sName);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowState(Qt::WindowMaximized);
	ui.labelSceneName->setText(m_pDev->m_pDevData->m_sDevName);
	ui.gridLayoutLog->addWidget(CLogOper::instance().createLogWidget(m_pDev->m_pDevData->m_sDevName));
	initSensorSeriesList();
	initTrandChart();
}

void CSensorSeriesMain::initConnect()
{
	connect(ui.pushButtonDeviceConfig, &QPushButton::clicked, this, &CSensorSeriesMain::slotPushDevConfig);
	connect(ui.pushButtonHistory, &QPushButton::clicked, this, &CSensorSeriesMain::slotPushHistoryQuery);
	connect(ui.pushButtonAlarmQuery, &QPushButton::clicked, this, &CSensorSeriesMain::slotPushAlarmQuery);
}

void CSensorSeriesMain::setStyleSheetFile()
{
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void CSensorSeriesMain::initSensorSeriesList()
{
	CSensorSeriesList* pWidget = new CSensorSeriesList(m_pDev);
	connect(pWidget, &CSensorSeriesList::sigChannelClicked, this, &CSensorSeriesMain::slotPushChannelClicked);
	ui.gridLayout->addWidget(pWidget);
}

void CSensorSeriesMain::initTrandChart()
{
	bool bFrist = true;
	CDevDataSensorSeries* pDevData = (CDevDataSensorSeries*)m_pDev->m_pDevData;
	for (QMap<int, SChannel>::iterator i = pDevData->m_mapChannel.begin(); i != pDevData->m_mapChannel.end(); i++)
	{
		int nChannelNo = i.key();
		SChannel sChannel = i.value();
		CSensorSeriesTrandChart *pWidget = new CSensorSeriesTrandChart(m_pDev, nChannelNo);
		pWidget->hide();
		ui.verticalLayoutChart->addWidget(pWidget);
		m_mapChart.insert(pDevData->m_sDevGuid + "-" + QString::number(nChannelNo), pWidget);
	}
}

void CSensorSeriesMain::refreshData(bool bRefresh)
{
	if (bRefresh)
	{
		slotPushChannelClicked(m_sShowGuidNow);
	}
}

void CSensorSeriesMain::slotPushChannelClicked(QString sGuid)
{
	m_sShowGuidNow = sGuid;
	for (auto itor = m_mapChart.begin(); itor != m_mapChart.end(); itor++)
	{
		itor.value()->hide();
	}
	if (m_mapChart.contains(sGuid))
	{
		m_mapChart[sGuid]->show();
		m_mapChart[sGuid]->slotQuery();
	}
}

void CSensorSeriesMain::slotPushDevConfig()
{
	CSensorSericesCfgOper oper(m_pDev->m_pDevData);
	oper.exec();
}

void CSensorSeriesMain::slotPushHistoryQuery()
{
	CSensorSeriesHistory oper(m_pDev->m_sDevGuid);
	oper.exec();
}

void CSensorSeriesMain::slotPushAlarmQuery()
{
	CSensorSeriesAlarmQuery oper(m_pDev->m_sDevGuid);
	oper.exec();
}